const skinBehavior = require("../../../utils/themeSkin/skinBehavior")
var app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    dy_password: "",
    details: {},
    language: "",
    current_page: 1,
    last_page: 1,
    list: [],
    choose_index: 1,
    sort_by: 1, //1-综合；2-销量；3-佣金率；4-粉丝数。不填默认为1
    sort_type: 0, //0-降序 1-升序
    sort: {
      up: 0,
      down: 0
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData({
      page: 1
    });
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en,
    });
  },

  onShow: function () {

  },
  onReachBottom: function () {
    this.getMoreData()
  },
  goods_3(arr) {
    if (arr && arr.length >= 1) {
      return arr.slice(0, 3)
    }
    return []
  },
  link(e) {
    wx.setStorageSync('tiktok_detail',e.currentTarget.dataset.item)
    wx.navigateTo({
      url: '/packageF/cps/tiktok_room/tiktok_room'
    })
  },
  getData(json) {
    app._getNetWork({
      url: app.getNetAddresss("plugin.tiktok-cps.api.live.live-list"),
      data: json,
      success: resdata => {
        let data = resdata.data;
        if (data.result == 0) {
          return app.tips(data.msg)
        }
        if (data.data.live_infos && data.data.live_infos.length !== 0) {
          data.data.live_infos.forEach((item, i) => {
            if (item.products && item.products.length !== 0) {
              item.goods = item.products.slice(0, 3)
            }
          })
          if (json.page == 1) {
            this.setData({
              list: data.data.live_infos
            })
          } else if (json.page > 1) {
            let list = this.data.list.concat(data.data.live_infos)
            this.setData({
              list
            })
          }

        }

        this.setData({
          current_page: data.data.current_page,
          last_page: data.data.last_page,
        })
      }
    })
  },
  onClickHide() {
    this.setData({
      show: false
    });
  },
  code_link() {
    wx.setClipboardData({
      data: this.data.dy_password,
      success: () => {
        app.tips('口令已复制，打开抖音app下单！')
        this.setData({
          show: false
        })
      },
      fail: (err) => {
        app.tips('复制失败')
      }
    });
  },
  copy_handle(e) {
    let item = e.currentTarget.dataset.item;
    app._postNetWork({
      url: app.getNetAddresss("plugin.tiktok-cps.api.live.tranfer-link"),
      data: {
        openid: item.author_openid
      },
      success: resdata => {
        if (resdata.data.result == 0) {
          return app.tips(resdata.data.msg);
        }
        this.setData({
          details: item,
          dy_password: resdata.data.data.dy_password,
          show: true
        })
      }
    })
  },
  getMoreData() {
    if (this.data.current_page >= this.data.last_page) {
      return;
    }
    let page = Number(this.data.current_page) + 1;
    this.getData({
      page,
      sort_type: this.data.sort_type,
      sort_by: this.data.sort_by
    })

  },
  choose(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      choose_index: index
    })
    if (Number(index) !== 1) {
      if (this.data.sort.up == 0 && this.data.sort.down == 0) {
        this.setData({
          "sort.up": 1,
          "sort.down": 0
        })
      } else if (this.data.sort.up == 1 && this.data.sort.down == 0) {
        this.setData({
          "sort.up": 0,
          "sort.down": 1
        })
      } else if (this.data.sort.up == 0 && this.data.sort.down == 1) {
        this.setData({
          "sort.up": 1,
          "sort.down": 0
        })
      }
      if (this.data.sort.down == 1) {
        this.setData({
          sort_type: 0
        })
      }
      if (this.data.sort.up == 1) {
        this.setData({
          sort_type: 1
        })
      }
    } else if (index == 1) {
      this.setData({
        "sort.up": 0,
        "sort.down": 0
      })
    }

    let sort_by = index;
    this.setData({
      sort_by
    })
    this.getData({
      page: 1,
      sort_type: this.data.sort_type,
      sort_by
    })
  },
})